package com.example.administrator.recyclerviewtest_407xjy.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.administrator.recyclerviewtest_407xjy.adapter.TwareAdapter;
import com.example.administrator.recyclerviewtest_407xjy.R;
import com.example.administrator.recyclerviewtest_407xjy.bean.TwareBean;
import com.example.administrator.recyclerviewtest_407xjy.iface.TwareListener;
import com.example.administrator.recyclerviewtest_407xjy.model.TwareModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private TwareAdapter twareAdapter;//适配器
    private List<TwareBean> list=null;//数据源

    TwareListener twareListener =new TwareListener() {
        @Override
        public void onResponse(List<TwareBean> beanList) {
            list=beanList;
            twareAdapter.setList(list);//传给adaoter
            twareAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TwareModel twareModel =new TwareModel();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        twareModel.getReaultList("tware",2,"rv1l2tidbomann2qn86nln6ur2", twareListener);
    }

    private void initRecyclerView() {
        //获取recyclerview
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        //线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度 提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建adaper
        //list=getData();
        twareAdapter =new TwareAdapter(this);
        twareAdapter.setList(list);
        //绑定recycle和adapter
        recyclerView.setAdapter(twareAdapter);



    }
/*
    //自定义
    private List<TwareBean> getData()
    {

        List<TwareBean> list=new ArrayList<TwareBean>();
        TwareBean articleBean;
        //第一组
        articleBean=new TwareBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);
        //第二组
        articleBean=new TwareBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("标题2");
        articleBean.setDescription("描述2");
        articleBean.setTime("2017-4-2");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);
        //第三组
        articleBean=new TwareBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("标题3");
        articleBean.setDescription("描述3");
        articleBean.setTime("2017-4-3");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);
        //第四组
        articleBean=new TwareBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题4");
        articleBean.setDescription("描述4");
        articleBean.setTime("2017-4-4");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);
        //第五组
        articleBean=new TwareBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题5");
        articleBean.setDescription("描述5");
        articleBean.setTime("2017-4-5");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);
        //第六组
        articleBean=new TwareBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("标题6");
        articleBean.setDescription("描述6");
        articleBean.setTime("2017-4-6");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);
        //定义一个LIST 赋值 返回
        return list;
    }
    */
}
